package com.example.administrator.kotlin.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import com.example.administrator.kotlin.R
import com.example.administrator.kotlin.fragment.FirstPageFragment
import com.example.administrator.kotlin.fragment.NewsFragment
import kotlinx.android.synthetic.main.activity_view_pager.*

class ViewPagerActivity : AppCompatActivity() {
    var pageradapter: MyAdapter? = null
    var firstPageFragment: FirstPageFragment = FirstPageFragment()
    var newsFragment: NewsFragment = NewsFragment()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager)
        val fragments = arrayOf(firstPageFragment, newsFragment)
        val titles = arrayOf("News", "妹纸")
        pageradapter = MyAdapter(supportFragmentManager, fragments, titles)
        viewpager.adapter = pageradapter
        tablayout.setupWithViewPager(viewpager)

    }

    class MyAdapter(supportFragmentManager: FragmentManager?, var fragments: Array<Fragment>, var titles: Array<String>) : FragmentPagerAdapter(supportFragmentManager) {
        override fun getItem(position: Int): Fragment? {
            println("tag" + fragments.size)
            return fragments.get(position)
        }

        override fun getCount(): Int {
            return fragments.size
        }

        override fun getPageTitle(position: Int): CharSequence {
            return titles.get(position)
        }

    }
}
